FROM openjdk:8u171-jdk

# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
RUN groupadd -r jetty && useradd -r -g jetty jetty

ENV JETTY_HOME /usr/local/jetty
ENV PATH $JETTY_HOME/bin:$PATH
RUN mkdir -p "$JETTY_HOME"
WORKDIR $JETTY_HOME

ENV JETTY_VERSION 9.2.22.v20170606
ENV JETTY_TGZ_URL https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-distribution/$JETTY_VERSION/jetty-distribution-$JETTY_VERSION.tar.gz

# GPG Keys are personal keys of Jetty committers (see https://github.com/eclipse/jetty.project/blob/0607c0e66e44b9c12a62b85551da3a0edce0281e/KEYS.txt)
ENV JETTY_GPG_KEYS \
	AED5EE6C45D0FE8D5D1B164F27DED4BF6216DB8F \
	2A684B57436A81FA8706B53C61C3351A438A3B7D \
	5989BAF76217B843D66BE55B2D0E1FB8FE4B68B4 \
	B59B67FD7904984367F931800818D9D68FB67BAC \
	BFBB21C246D7776836287A48A04E0C74ABB35FEA \
	8B096546B1A8F02656B15D3B1677D141BCF3584D \
	FBA2B18D238AB852DF95745C76157BDF03D0DCD6 \
	5C9579B3DB2E506429319AAEF33B071B29559E1E

RUN set -xe \
	&& curl -SL "$JETTY_TGZ_URL" -o jetty.tar.gz \
	&& curl -SL "$JETTY_TGZ_URL.asc" -o jetty.tar.gz.asc \
	&& export GNUPGHOME="$(mktemp -d)" \
	&& gpg --keyserver ha.pool.sks-keyservers.net --recv-keys AED5EE6C45D0FE8D5D1B164F27DED4BF6216DB8F \
	&& gpg --keyserver ha.pool.sks-keyservers.net --recv-keys 2A684B57436A81FA8706B53C61C3351A438A3B7D \
	&& gpg --keyserver ha.pool.sks-keyservers.net --recv-keys 5989BAF76217B843D66BE55B2D0E1FB8FE4B68B4 \
	&& gpg --keyserver ha.pool.sks-keyservers.net --recv-keys B59B67FD7904984367F931800818D9D68FB67BAC \
	&& gpg --keyserver ha.pool.sks-keyservers.net --recv-keys BFBB21C246D7776836287A48A04E0C74ABB35FEA \
	&& gpg --keyserver ha.pool.sks-keyservers.net --recv-keys 8B096546B1A8F02656B15D3B1677D141BCF3584D \
	&& gpg --keyserver ha.pool.sks-keyservers.net --recv-keys FBA2B18D238AB852DF95745C76157BDF03D0DCD6 \
	&& gpg --keyserver ha.pool.sks-keyservers.net --recv-keys 5C9579B3DB2E506429319AAEF33B071B29559E1E \
	&& gpg --batch --verify jetty.tar.gz.asc jetty.tar.gz \
	&& rm -rf "$GNUPGHOME" \
	&& tar -xvf jetty.tar.gz --strip-components=1 \
	&& sed -i '/jetty-logging/d' etc/jetty.conf \
	&& rm -fr demo-base javadoc \
	&& rm jetty.tar.gz* \
	&& rm -rf /tmp/hsperfdata_root

ENV JETTY_BASE /var/lib/jetty
RUN mkdir -p "$JETTY_BASE"
WORKDIR $JETTY_BASE

# Get the list of modules in the default start.ini and build new base with those modules, then add setuid
RUN modules="$(grep -- ^--module= "$JETTY_HOME/start.ini" | cut -d= -f2 | paste -d, -s)" \
	&& set -xe \
	&& java -jar "$JETTY_HOME/start.jar" --add-to-startd="$modules,setuid" \
	&& chown -R jetty:jetty "$JETTY_BASE" \
	&& rm -rf /tmp/hsperfdata_root

ENV TMPDIR /tmp/jetty
RUN set -xe \
	&& mkdir -p "$TMPDIR" \
	&& chown -R jetty:jetty "$TMPDIR"

COPY docker-entrypoint.sh /

EXPOSE 8080
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["java","-jar","/usr/local/jetty/start.jar"]